package com.crm.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.mapper.ActivityMapper;
import com.crm.pojo.common.PageParams;
import com.crm.pojo.common.Params;
import com.crm.pojo.domain.Activity;
import com.crm.pojo.domain.Channel;
import com.crm.pojo.dto.ActivityDto;
import com.crm.pojo.dto.ActivityListDto;
import com.crm.pojo.vo.ActivityListVo;
import com.crm.service.ActivityService;
import com.crm.util.RandomStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 活动管理分页查询
     *
     * @param listDto
     * @return
     */
    @Override
    public PageParams<ActivityListVo> list(ActivityListDto listDto) {
        log.info("活动管理分页查询,控制层传来:{}", listDto);
        //分页查询总记录数
        LocalDateTime beginTime = null;
        LocalDateTime endTime = null;
        LocalDateTime beginCreateTime = null;
        LocalDateTime endCreateTime = null;
        if (!CollectionUtils.isEmpty(listDto.getParams())) {
            Map<String, String> params = listDto.getParams();
            String beginTimeStr = params.get("beginTime");
            String endTimeStr = params.get("endTime");
            if (StringUtils.hasText(beginTimeStr) && StringUtils.hasText(endTimeStr)) {
                beginTime = LocalDate.parse(beginTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
                endTime = LocalDate.parse(endTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
//                beginTime = LocalDateTime.parse(beginTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//                 endTime = LocalDateTime.parse(beginTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
            String beginCreateTimeStr = params.get("beginCreateTime");
            String endCreateTimeStr = params.get("endCreateTime");
            if (StringUtils.hasText(beginCreateTimeStr) && StringUtils.hasText(endCreateTimeStr)) {
                beginCreateTime = LocalDate.parse(beginCreateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
                endCreateTime = LocalDate.parse(endCreateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
//                 beginCreateTime = LocalDateTime.parse(beginCreateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//                 endCreateTime = LocalDateTime.parse(beginCreateTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
        }
        Long total = activityMapper.listCount(
                listDto.getCode(), listDto.getChannel(),
                beginTime, endTime, beginCreateTime, endCreateTime);
        log.info("总记录数:{}", total);
        //分页查询总页数 总页数=总记录数/每页显示的记录数 +1
        Long pages = total > 0 ? (total + listDto.getPageSize() - 1) / listDto.getPageSize() : 0;
        //起始索引
        int startIndex = (listDto.getPageNum() - 1) * listDto.getPageSize();
        //分页查询
        List<ActivityListVo> list = activityMapper.selList(startIndex, listDto.getPageSize(),
                listDto.getCode(), listDto.getChannel(),
                beginTime, endTime, beginCreateTime, endCreateTime);
        log.info("分页查询结果:{}", total + "条记录");
        return PageParams.<ActivityListVo>builder()
                .code(200)
                .msg("查询成功")
                .total(total)
                .rows(list)
                .params(new Params(total, (long) listDto.getPageSize(), 0L, 0L, 0L))
                .build();
    }

    /**
     * 渠道查询
     *
     * @return
     */
    @Override
    public List<Channel> selChannel() {
        return activityMapper.selChannel();
    }

    /**
     * 新增活动
     *
     * @param dto
     */
    @Override
    public void add(ActivityDto dto) {
        log.info("新增活动,控制层传来:{}", dto);
        Activity activity = new Activity();
        BeanUtils.copyProperties(dto, activity);
        activity.setCode(RandomStringUtil.generateRandomString());
        activityMapper.insertActivity(activity);
    }

    /**
     * 修改活动
     *
     * @param dto
     */
    @Override
    public void update(ActivityDto dto) {
        log.info("修改活动,控制层传来:{}", dto);
        Activity activity = new Activity();
        BeanUtils.copyProperties(dto, activity);
        activityMapper.updateActivity(activity);
    }

    /**
     * 回显活动数据
     *
     * @return
     */
    @Override
    public Activity selOne(Integer id) {
        log.info("回显活动数据,控制层传来活动id:{}", id);
        return activityMapper.selOne(id);
    }

    /**
     * 删除活动
     *
     * @param id
     */
    @Override
    public void del(Integer id) {
        log.info("删除活动,控制层传来:{}", id);
        activityMapper.delActivity(id);
    }


}
